const App = getApp();
const QQMapWX = require('../../../../utils/qqmap-wx-jssdk.min.js');
const wxMap = new QQMapWX({
  key: 'WFVBZ-3SMRW-NDQRH-O6IZR-VBHJZ-JFFZU' // 必填
});
Component({
  properties: {
    show: {
      type: Boolean,
      value: !0
    },
    messageCount: {
      type: String,
      value: ""
    },
    placeholder: {
      type: String,
      value: ""
    }
  },
  data: {
    statusBarHeight: "",
    statusBarPadding: "",
    city: '定位'
  },
  lifetimes: {
    attached() {
      this.setData({
        statusBarHeight: App.globalData.navHeight,
      })
      this.setData({
        statusBarPadding: App.globalData.navTop,
      })
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userLocation']) {
            /**经纬度逆解析 */
            this.reverseGeocoder()
          }
        }
      })
    }
  },
  methods: {
    /**经纬度逆解析 */
    reverseGeocoder() {
      const that = this;
      wx.getLocation({
        type: 'gcj02',
        altitude: true,
        success(res) {
          var latitude = res.latitude
          var longitude = res.longitude
          wxMap.reverseGeocoder({
            location: {
              latitude: latitude,
              longitude: longitude
            },
            success(res) {
              const city = res.result.address_component.city.replace(/[市]/g,'')
              that.setData({
                city: city.length>2? city.slice(0,2) + '...' : city
              })
            },
            fail(res) {
              console.log(res,"fail2")
            }
          });
        },
        fail(res) {
          console.log(res,"fail")
        }
      })
    },
    linkToSearch: function () {
      wx.navigateTo({
        url: "/pages/index/sonpages/search/index"
      });
    },
    toggCity() {
      this.reverseGeocoder()
      return
      wx.navigateTo({
        url: "/pages/index/sonpages/toggle-city/index"
      });
    },
    linkToNews: function () {
      wx.navigateTo({
        url: "/pages/index/sonpages/informations/index"
      });
    }
  }
});